<template>
    <div>
        <h1>用户理财</h1>
        <table class="table table-bordered" border="1">
            <tr>
                <td>理财名称</td>
                <td>{{F_Lname}}</td>
            </tr>
            <tr>
                <td>年收益</td>
                <td>{{F_Lyser}}</td>
            </tr>
            <tr>
                <td>利息</td>
                <td>
                    <select v-model="F_Iid">
                        <option value="0">请选择</option>
                        <option v-for="item in info" :key="item.F_Iid" :value="item.F_Iid">{{item.F_Imoney}}</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>最低金额</td>
                <td>{{F_Lmin}}</td>
            </tr>
            <tr>
                <td>理财金额</td>
                <td>
                    <input type="text" v-model="F_Umoney"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <button class="btn btn-primary" @click="LoginAdd()">理财</button>
                </td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref,onMounted } from 'vue';

const F_Lname=ref('');
const F_Lyser=ref('');
const F_Lmin=ref('');
const F_Iid=ref(0);
const F_Umoney=ref('');

const info:any= ref([]);
import axiox from 'axios';
import { useRoute} from 'vue-router';
import { useStore } from '../../store/indext';

const route = useRoute();
const store = useStore();


const id=route.params.id;

onMounted(()=>{
    Loginint();
    LoginBind();
})

const Loginint = () => {
    axiox({
        url: 'http://localhost:51873/api/User/FiniaShowMoney',
        method: 'get',
        params: {
            id:id,
        }
    })
    .then(res => {
        F_Lname.value=res.data.F_Lname;
        F_Lyser.value=res.data.F_Lyser;
        F_Lmin.value=res.data.F_Lmin;
        console.log(res.data);
    })
    .catch(err => {
        console.log(err);
    })
}

const LoginBind = () => {
    axiox({
        url: 'http://localhost:51873/api/User/FiniaBind',
        method: 'get',
    })
    .then(res => {
        info.value=res.data;
        console.log(res.data);
    })
    .catch(err => {
        console.log(err);
    })
}
const LoginAdd = () => {
    if(F_Lmin.value<F_Umoney.value)
    {
        alert('金额不能大于最低金额');
        return;
    }


    axiox({
        url: 'http://localhost:51873/api/User/FiniaUserAdd',
        method: 'post',
        data: {
            F_Iid:F_Iid.value,
            F_Umoney:F_Umoney.value,
            U_Lid:store.id
        }
        
    })
    .then(res => {
        console.log(res.data);
        if (res.data>0) {
            alert('理财成功');
        } 
        else {
            alert('理财错误')
            return;
        }
    })
    .catch(err => {
        console.log(err);
    })
}
</script>